package com.ctrip.frame;

import com.ctrip.business.PCOrderService;
import com.ctrip.constant.OrderStatusEnum;
import com.ctrip.task.OrderTask;
import com.go.commons.dto.Page;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class OrderFrame extends JFrame {
    private static final URL START_IMG = OrderFrame.class.getClassLoader().getResource("images/start.png");
    private static final URL STOP_IMG = OrderFrame.class.getClassLoader().getResource("images/stop.png");
    private static final int WIDTH = 1000;
    private static final int HEIGHT = 700;
    private OrderStatusEnum orderStatusEnum;

    JPanel panelTop, panelTable, panelPage;
    JLabel labelRefresh,labelOrderStatus, labelGrab;
    JTable table;
    JScrollPane jsp;
    JComboBox comboBoxOrderStatus;
    OrderTableModel model;
    Page page;
    JLabel firstPage, endPage, totalPage, lastPage, nextPage;


    public OrderFrame() {
        super("抢单中心");
        page = new Page(30, 1);
        orderStatusEnum = OrderStatusEnum.ALL;
        initGUI();
    }

//    public static void initJTable() {
//        List<CallMessage> messages = MessageService.list(new Page(10, 1), null, null, null, null);
//    }

    private void initGUI() {
        this.setLayout(null);

        panelTop = new JPanel();
        panelTop.setBounds(0, 2, WIDTH, 40);
        panelTop.setLayout(new GridBagLayout());

        labelOrderStatus = new JLabel("订单状态：");
        comboBoxOrderStatus = new JComboBox();
        comboBoxOrderStatus.addItem("全部");
        comboBoxOrderStatus.addItem("待抢单");
        comboBoxOrderStatus.addItem("被抢单");
        comboBoxOrderStatus.addItem("已抢单");
        comboBoxOrderStatus.addItem("已过期");
        comboBoxOrderStatus.addItem("已取消");
        comboBoxOrderStatus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OrderStatusEnum orderStatus = OrderStatusEnum.find(String.valueOf(comboBoxOrderStatus.getSelectedIndex()));
                if (orderStatus != orderStatusEnum) {
                    orderStatusEnum = orderStatus;
                    page.setCurPage(1);
                    resetTable();
                }
            }
        });

        labelGrab = new JLabel("开始抢单");
        labelGrab.setForeground(Color.BLUE);
        labelGrab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        labelGrab.setVerticalAlignment(JLabel.CENTER);
        labelGrab.setIcon(new ImageIcon(START_IMG));
        labelGrab.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String text = labelGrab.getText();
                if (text.equals("开始抢单")) {
                    labelGrab.setIcon(new ImageIcon(STOP_IMG));
                    labelGrab.setText("结束抢单");
                    labelGrab.setForeground(Color.RED);
                    OrderTask.start();
                } else {
                    labelGrab.setIcon(new ImageIcon(START_IMG));
                    labelGrab.setText("开始抢单");
                    labelGrab.setForeground(Color.BLUE);
                    OrderTask.stop();
                }
            }
        });
//        labelStopGrab = new JLabel("结束抢单");
//        labelStopGrab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//        labelStopGrab.setVerticalAlignment(JLabel.CENTER);
        labelRefresh = new JLabel("刷新");
        labelRefresh.setSize(100, 40);
        labelRefresh.setVerticalAlignment(JLabel.CENTER);
        labelRefresh.setIcon(new ImageIcon(OrderFrame.class.getClassLoader().getResource("images/refresh.png")));
        labelRefresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        labelRefresh.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goFirstPage();
            }
        });

        panelTop.add(labelOrderStatus,new GBC(0, 0, 1, 1)
                .setIpad(0, 0)
                .setWeight(0, 0));
        panelTop.add(comboBoxOrderStatus,new GBC(1, 0, 1, 1)
                .setIpad(0, 0)
                .setWeight(0, 0)
                .setFill(GBC.BOTH)
                .setInsets(5, 5, 5, 5));
        panelTop.add(labelGrab,new GBC(2, 0, 1, 1)
                .setIpad(0, 0)
                .setWeight(0, 0)
                .setFill(GBC.BOTH)
                .setInsets(5, 50, 5, 5));
        panelTop.add(labelRefresh,new GBC(3, 0, 1, 1)
                .setIpad(0, 0)
                .setWeight(0, 0)
                .setFill(GBC.BOTH)
                .setInsets(5, 50, 5, 5));

        panelTable = new JPanel();
        panelTable.setBounds(0, 45, WIDTH, 550);

        model = new OrderTableModel();
        table = new JTable(model);

//        table.setBackground(Color.YELLOW);

//        table.setTableHeader(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        jsp = new JScrollPane(table);
        jsp.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jsp.setPreferredSize(new Dimension(WIDTH - 100, 520));

        panelTable.add(jsp);

        panelPage = new JPanel();
        panelPage.setBounds(0, 610, WIDTH, 50);
        firstPage = new JLabel();
        endPage = new JLabel();
        lastPage = new JLabel();
        nextPage = new JLabel();
        totalPage = new JLabel();
        firstPage.setPreferredSize(new Dimension(20, 30));
        endPage.setPreferredSize(new Dimension(20, 30));
        lastPage.setPreferredSize(new Dimension(20, 30));
        nextPage.setPreferredSize(new Dimension(20, 30));
        totalPage.setPreferredSize(new Dimension(50, 30));
        totalPage.setHorizontalAlignment(JLabel.CENTER);
        firstPage.setIcon(new ImageIcon(OrderFrame.class.getClassLoader().getResource("images/go-first-view.png")));
        endPage.setIcon(new ImageIcon(OrderFrame.class.getClassLoader().getResource("images/go-end-view.png")));
        lastPage.setIcon(new ImageIcon(OrderFrame.class.getClassLoader().getResource("images/go-previous-view.png")));
        nextPage.setIcon(new ImageIcon(OrderFrame.class.getClassLoader().getResource("images/go-next-view.png")));
        firstPage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        endPage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        nextPage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        lastPage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        firstPage.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (page.getCurPage() != 1) {
                    goFirstPage();
                }
            }
        });
        endPage.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (page.getCurPage() != page.getTotalPage()) {
                    page.setCurPage(page.getTotalPage());
                    resetTable();
                }
            }
        });
        nextPage.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (page.getCurPage() < page.getTotalPage()) {
                    page.setCurPage(page.getCurPage() + 1);
                    resetTable();
                }
            }
        });
        lastPage.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (page.getCurPage() > 1) {
                    page.setCurPage(page.getCurPage() - 1);
                    resetTable();
                }
            }
        });

        panelPage.add(firstPage);
        panelPage.add(lastPage);
        panelPage.add(totalPage);
        panelPage.add(nextPage);
        panelPage.add(endPage);

        goFirstPage();

        Container container = getContentPane();

        container.add(panelTop);
        container.add(panelTable);
        container.add(panelPage);
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setLocationRelativeTo(null);
//        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = this.getToolkit().getImage(getClass().getClassLoader().getResource("images/logo.png"));
        this.setIconImage(image);

        //debug
        panelTop.setBorder(BorderFactory.createEtchedBorder());
//        panelTable.setBorder(BorderFactory.createLineBorder(Color.black));
        jsp.setBorder(BorderFactory.createLineBorder(Color.WHITE));
    }

    public void goFirstPage() {
        page.setCurPage(1);
        resetTable();
    }

    public void resetTable() {
        model.setData(PCOrderService.listOrders(page, orderStatusEnum));
        totalPage.setText(page.getCurPage() + "/" + page.getTotalPage());
        if (page.getCurPage() == 1) {
            firstPage.setEnabled(false);
            lastPage.setEnabled(false);
        } else if (!firstPage.isEnabled()) {
            firstPage.setEnabled(true);
            lastPage.setEnabled(true);
        }
        if (page.getCurPage() == page.getTotalPage()) {
            nextPage.setEnabled(false);
            endPage.setEnabled(false);
        } else if (!firstPage.isEnabled()) {
            nextPage.setEnabled(true);
            endPage.setEnabled(true);
        }
    }

}
